package it.unibo.mumu;

import java.util.ArrayList;
import java.util.Iterator;

import it.unibo.mumu.entity.Planet;
import it.unibo.mumu.entity.PlanetId;
import it.unibo.mumu.util.SingletonSupport;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
/***
 * Classe layout che viene utilizzata per disegnare i suggerimenti una volta usciti da un pianeta
 */
public class HintsLayout extends RelativeLayout{
	
	private ArrayList <Paint> paints;
	private ArrayList <Path> paths;
	private ArrayList <Point> destPoints;
	private Planet lastPlanet;
	
	public HintsLayout(Context context) {
		super(context);
//		mPath = new Path();
//		mPath.moveTo(0, 0);
//		mPath.lineTo(300, 300);
////	    mPath.quadTo(100, 200, 100, 200); 
//	       
//		paint = new Paint();
//		paint.setColor(Color.rgb(255, 0, 0));
//		
//		paint.setStyle(Style.STROKE);
//		paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));
////		paint.setStrokeWidth(5);
	}
	
	public HintsLayout(Context context, AttributeSet attrs){
		super(context, attrs);
	}
	
	public HintsLayout(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
	}
	
	public void onDraw(Canvas canvas) {

        if(paths != null){
	        for(int i = 0; i < paths.size(); i++){
	        	canvas.drawPath(paths.get(i), paints.get(i));
//	        	canvas.drawLine(destPoints.get(i).x, destPoints.get(i).y, lastPlanet.getCanvasCoordX(), lastPlanet.getCanvasCoordY(), paints.get(i));
	        }
        }
	}

	public void drawHintsFromLastPlanet() {
		int lastPlanetId = SingletonSupport.getInstance().getLastPlanetVisitedID();
		if(lastPlanetId >= 0 || GalaxyActivity.DEBUG){
			
			int universeId = SingletonSupport.getInstance().getRealUniverseID();
			lastPlanet = SingletonSupport.getInstance().getUniverses().get(universeId).getPlanets().get(lastPlanetId);
			
//			startPoint = new Point(Math.round(lastPlanet.getCoordX()), Math.round(lastPlanet.getCoordY()));
			paints = new ArrayList<Paint>();
			paths = new ArrayList<Path>();
			destPoints = new ArrayList<Point>();
			
			Iterator<PlanetId> it = lastPlanet.getConnections().iterator();
			PlanetId currentConnection;
			Paint currentPaint;
			Path currentPath;
			Planet currentEndPlanet;
			while(it.hasNext()){
				// imposto colore e stile del path
				currentConnection = it.next();
				currentPaint = new Paint();
				currentPaint.setStyle(Style.STROKE);
				currentPaint.setPathEffect(new DashPathEffect(new float[] {5,10}, 0));
				currentPaint.setColor(Color.rgb(currentConnection.getR(),currentConnection.getG(),currentConnection.getB()));
				currentPaint.setStrokeWidth(2);
				paints.add(currentPaint);
				// imposto la destinazione del path
				int realEndPlanetID = SingletonSupport.getInstance().findRealPlanetId(currentConnection.getPlanetId());
				currentEndPlanet = SingletonSupport.getInstance().getUniverses().get(universeId).getPlanets().get(realEndPlanetID);
				currentPath = new Path();
				currentPath.moveTo(lastPlanet.getCanvasCoordX()+65, lastPlanet.getCanvasCoordY()+20);
				currentPath.lineTo(currentEndPlanet.getCanvasCoordX()+65, currentEndPlanet.getCanvasCoordY()+20);
				paths.add(currentPath);
				destPoints.add(new Point(Math.round(currentEndPlanet.getCanvasCoordX()), Math.round(currentEndPlanet.getCanvasCoordY())));
			}
		}
		
	}

}
